<?php
/**
 * Created by PhpStorm.
 * User: zhaoyi
 * Date: 2022/1/26
 * Time: 11:46.
 */

declare(strict_types=1);

namespace App\Application\Api\Controller;

use App\Annotation\Api;
use App\Application\Admin\Model\Order;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;

use App\Application\Admin\Model\Roomlabel;
use App\Application\Admin\Service\AdminSettingService;
use App\Application\Admin\Service\CityService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;

#[Controller("/api/Recharge")]
class RechargeController extends AbstractController
{
    #[Inject]
    protected AdminSettingService $setting;

    #[Api]
    #[PostMapping("index")]
    function index()
    {
        $store_id = $this->request->input('store_id', '');
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }

        $vip = Db::table('vip')->where($where)->get();
        return compact('vip',);
    }

    #[Api]
    #[PostMapping("detail")]
    function detail()
    {
        $id = $this->request->input('id', '');
        $where = [];
        if ($id) {
            $where['goods_id'] = $id;
        }
        $class = Db::table('goods_type')->pluck('name', 'goods_type_id');
        $goods = Db::table('goods')->where($where)->first();
        return compact('class', 'goods');
    }

    #[Api]
    #[PostMapping("rechargeOrder")]
    function rechargeOrder()
    {
        $user = $this->checkToken();
        if (empty($user)) {
            return $this->returnErrorJson('请先登录');
        }
        $vip_id = $this->request->input('id', '');

        $where = [];
        if ($vip_id) {
            $where['vip_id'] = $vip_id;
            $goods = Db::table('vip')->where($where)->first();
        };
        $data = array(
            'order_on' => $this->orderOn(),
            'user_id' => $user['member_id'],
            'price' => $goods->price ?? $this->request->input('chargeprice', '0'),
            'give_price' => $goods->give_price ?? 0,
            'vip_id' => $goods->goods_id ?? 0,
            'store_id' => $goods->store_id ?? 0,

        );

        $class = Db::table('vip_order')->insert($data);
        return compact('data', 'user');
    }

    #[Api]
    #[PostMapping("orderDetail")]
    function orderDetail()
    {
        $user = $this->checkToken();
        if (empty($user)) {
            return $this->returnErrorJson('请先登录');
        }
        $order_on = $this->request->input('order_on', '');
        $order = Db::table('product_order')->where('order_on', $order_on)->first() ?: [];
        if ($order->province_id) {
            $order->address_lan = ['长沙'];
        } else {
            $order->address_lan = false;
        }

        $goods = Db::table('goods')->where('goods_id', $order->goods_id)->first();
        $order->image_url = json_decode($goods->image_url);
        $order->name = $goods->name;

        return compact('order', 'goods');
    }

    #[Api]
    #[PostMapping("updateOrder")]
    function updateOrder()
    {
        $user = $this->checkToken();
        if (empty($user)) {
            return $this->returnErrorJson('请先登录');
        }
        $address_id = $this->request->input('address_id', '');
        $order_on = $this->request->input('order_on', '');
        $addressModel = Db::table('user_address')->where('id', $address_id)->first() ?: [];

        $orderData = array(
            'province_id' => $addressModel->province_id,
            'city_id' => $addressModel->city_id,
            'area_id' => $addressModel->area_id,
            'address_details' => $addressModel->address_details,
            'realname' => $addressModel->realname,
            'mobile' => $addressModel->mobile,
            'update_time' => time(),
        );


        if (Db::table('product_order')->where('order_on', $order_on)->update($orderData)) {

            $res = Db::table('product_order')->where('order_on', $order_on)->first() ?: [];
            return compact('res');
        }


    }

}